#
# Makfile for building crypto test application
#

CC	 			= /usr/bin/g++
CFLAGS	 		= -c -Wall -g 
LDFLAGS			= -ldl
SRC_DIR 		= src
INC_DIR			= include
INCLUDE_PATH 	= -I$(INC_DIR)
INC_FILES		= $(INC_DIR)/trace.h \
				  $(INC_DIR)/functions.h \
				  $(INC_DIR)/util.h
LIB_DIR			= lib
SOURCES			= $(SRC_DIR)/inv_mod.cpp \
				  $(SRC_DIR)/inv_mod_bn.cpp \
				  $(SRC_DIR)/exp_mod_bn.cpp \
				  $(SRC_DIR)/gcd.cpp \
				  $(SRC_DIR)/crt.cpp \
				  $(SRC_DIR)/dsa.cpp \
				  $(SRC_DIR)/sha1.cpp \
				  $(SRC_DIR)/sign.cpp \
				  $(SRC_DIR)/verif_sig.cpp \
				  $(SRC_DIR)/base64.cpp \
				  $(SRC_DIR)/quad_res.cpp \
				  $(SRC_DIR)/main.cpp 
OBJECTS 		= $(SOURCES:.cpp=.o)	  
TARGET	 		= crypto.exe

UNAME := $(shell uname -s |cut -f 1 -d "_")

ifeq ($(UNAME), CYGWIN)
# dynamic libraries with debugging symbols for windoze
OSSL_LIB		= $(LIB_DIR)/ssleay32.dll $(LIB_DIR)/libeay32.dll   	
endif
ifeq ($(UNAME), Linux)
# static lib with debugging symbols for linux
OSSL_LIB 		= $(LIB_DIR)/libcrypto.a
endif

all: $(TARGET)

$(TARGET): $(OBJECTS)
	@echo Linking ...
	${CC} $(LDFLAGS) $(OBJECTS) -o $@ $(OSSL_LIB)
	@echo     

# Make all headers dependencies to all objects
$(OBJECTS): $(INC_FILES)

.cpp.o: 
	@echo Compiling $<  to  $@
	$(CC) $(CFLAGS) $(INCLUDE_PATH) $< -o $@ 
	@echo

install: 
	@echo Install bash completion functions.
	@echo \(requires admin permissions\)
	@echo
	@echo Replace binary name and copy completion file to etc
	cp bash_completion tmp
	sed -i 's/TARGET/$(TARGET)/g' tmp
	mv tmp /etc/bash_completion.d/$(TARGET)
	@echo
	@echo Everything inside the /etc/bash_completion.d directory will be loaded 
	@echo when the /etc/bash_completion file is sourced \(or loaded\)
	@echo This can be forced with 
	@echo source /etc/bash_completion.d/$(TARGET)
	
clean:
	@echo Cleaning.. 
	rm -f $(OBJECTS)
	
clean_all: clean
	@echo Cleaning.. 
	rm -f $(TARGET)
	
clean_items:
	@echo Cleaning keys and signatures
	rm -f *.pem *.sig
